import java.lang.reflect.Array;

/**
 * 846. 多关键字排序
 * 给定 n 个学生的学号(从 1 到 n 编号)以及他们的考试成绩，表示为(学号，考试成绩)，请将这些学生按考试成绩降序排序，若考试成绩相同，则按学号升序排序。
 * https://www.lintcode.com/problem/multi-keyword-sort/description?_from=cat
 *
 * 样例
 * 样例1
 *
 * 输入: array = [[2,50],[1,50],[3,100]]
 * 输出: [[3,100],[1,50],[2,50]]
 * 样例2
 *
 * 输入: array = [[2,50],[1,50],[3,50]]
 * 输出: [[1,50],[2,50],[3,50]]
 * @author jeymingwu
 * @date 2019/9/30 17:43
 */
public class L0846_Multi_Keyword_Sort {
    /**
     * @param array: the input array
     * @return: the sorted array
     */
    public int[][] multiSort(int[][] array) {
        // Write your code here
        for (int i = 0; i < array.length - 1; ++i) {
            for (int j = i + 1; j < array.length; ++j) {
                if (array[i][1] < array[j][1]) {
                    int[] tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
                else if (array[i][1] == array[j][1]) {
                    if (array[i][0] > array[j][0]) {
                        int[] tmp = array[i];
                        array[i] = array[j];
                        array[j] = tmp;
                    }
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        L0846_Multi_Keyword_Sort sort = new L0846_Multi_Keyword_Sort();
        int[][] nums = {{7, 66}, {4, 77}, {3, 63}, {5, 81}, {1, 88}, {9, 86}, {6, 88}, {2, 82}, {8, 55}, {10, 95}};
        int[][] nows = sort.multiSort(nums);
        for (int i = 0; i < nums.length; ++i) {
            System.out.print(nums[i][0] + "," + nums[i][1] + " ");
        }
        System.out.println();
        for (int i = 0; i < nows.length; ++i) {
            System.out.print(nows[i][0] + "," + nows[i][1] + " ");
        }
    }
}
